/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaEstacao.java
 *
 * Created on 08/04/2013, 22:25:27
 */
package br.com.fatecpg.locacaobicicletas;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Davi
 */
public class ConsultaEstacao extends javax.swing.JInternalFrame {

    /** Creates new form ConsultaEstacao */
    public ConsultaEstacao() {
        initComponents();
    }

    private void atualizarest() throws SQLException {
        DefaultTableModel model = (DefaultTableModel) estacaojTable.getModel();
        model.setRowCount(0);
        String sql = "select *from estacao";
        ResultSet rs = ConexãoBike.ConexãoJDBC.createStatement().executeQuery(sql);
        while (rs.next()) {
            model.addRow(new Object[]{
                        rs.getInt("cd_estacao"),
                        rs.getString("nm_estacao"),
                        rs.getString("nm_end_estacao"),
                        rs.getLong("cd_tel_estacao")});
        }
    }
    
    private void Excluirestacao() throws SQLException {
        if (estacaojTable.getSelectedRow() >= 0) {
            String item = estacaojTable.getValueAt(estacaojTable.getSelectedRow(), 0).toString();
            if (JOptionPane.showConfirmDialog(this, "Excluir Estacao " + item) == JOptionPane.YES_NO_OPTION) {
                String sql = "delete from estacao where cd_estacao =" + item;
                try {
                    ConexãoBike.ConexãoJDBC.createStatement().execute(sql);
                    this.atualizarest();
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(this, "Erro ao tentar excluir registro" + ex.getMessage(),
                            "Erro", JOptionPane.ERROR_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Registro não Encontrado.",
                    "Aviso", JOptionPane.WARNING_MESSAGE);
        }
    }
    private void alteraestacao() {
      DefaultTableModel modelo = (DefaultTableModel) estacaojTable.getModel();
        int linhas = estacaojTable.getRowCount();
        String SQL = "";
        
        for (int i = 0; i < linhas; i++) {
            int cod = Integer.parseInt(estacaojTable.getValueAt(i, 0).toString());
            String nmest = estacaojTable.getValueAt(i, 1).toString().toUpperCase();
            String endest = estacaojTable.getValueAt(i, 2).toString().toUpperCase();
            String telest = estacaojTable.getValueAt(i, 3).toString();
           
            SQL = "update estacao " + "set nm_estacao = '" + nmest + "'"
            + ",nm_end_estacao = '"+ endest +"'"
            + ",cd_tel_estacao = "+ telest +""
            + "where cd_estacao = " + cod;
            
            try {
                ConexãoBike.ConexãoJDBC.createStatement().execute(SQL);
                //JOptionPane.showMessageDialog(null, "Alterações Efetuadas com Sucesso");
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Campos Não Alterados. Erro : "
                        + ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
        JOptionPane.showMessageDialog(null, "Alterações Efetuadas com Sucesso");
         }   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        consultarjButton = new javax.swing.JButton();
        excluirjButton = new javax.swing.JButton();
        alterarjButton = new javax.swing.JButton();
        cancelarrjButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        estacaojTable = new javax.swing.JTable();

        setIconifiable(true);
        setMaximizable(true);
        setTitle("Consulta de Estações");

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        consultarjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        consultarjButton.setText("CONSULTAR");
        consultarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultarjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(consultarjButton);

        excluirjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        excluirjButton.setText("EXCLUIR");
        excluirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(excluirjButton);

        alterarjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        alterarjButton.setText("ALTERAR");
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(alterarjButton);

        cancelarrjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        cancelarrjButton.setText("CANCELAR");
        cancelarrjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarrjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarrjButton);

        estacaojTable.setAutoCreateRowSorter(true);
        estacaojTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Estação", "Endereço", "Telefone"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Long.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        estacaojTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(estacaojTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void consultarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultarjButtonActionPerformed
    try {
        atualizarest();
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(this, "Erro:" + ex.getMessage(), "erro", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_consultarjButtonActionPerformed

private void excluirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirjButtonActionPerformed
  
try {
        Excluirestacao();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "ERRO" +ex.getMessage(),
                "ERRO", JOptionPane.ERROR_MESSAGE);
    }

    // TODO add your handling code here:
}//GEN-LAST:event_excluirjButtonActionPerformed

private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
alteraestacao();
// TODO add your handling code here:
}//GEN-LAST:event_alterarjButtonActionPerformed

private void cancelarrjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarrjButtonActionPerformed
this.setVisible(false);// TODO add your handling code here:
}//GEN-LAST:event_cancelarrjButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JButton cancelarrjButton;
    private javax.swing.JButton consultarjButton;
    private javax.swing.JTable estacaojTable;
    private javax.swing.JButton excluirjButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
